package edu.chl.jadetalk.control;

import edu.chl.jadetalk.business.ImageManagerBeanLocal;
import edu.chl.jadetalk.core.User;
import edu.chl.jadetalk.user.UserSessionBean;
import edu.chl.jadetalk.view.CommentBackingBean;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

/**
 *
 * @author jesper
 */
@ManagedBean
@RequestScoped
public class CommentingControlBean {

    @ManagedProperty(value = "#{userSessionBean}")
    private UserSessionBean userSessionBean;

    @EJB
    private ImageManagerBeanLocal imageManagerBean;
    
    @ManagedProperty(value="#{commentBackingBean}")
    private CommentBackingBean commentBackingBean;

    public CommentingControlBean() {
        
    }
    
    public String send(){
        
        Logger.getAnonymousLogger().log(Level.INFO, "Sending comment");
        Logger.getAnonymousLogger().log(Level.INFO, String.format("Logged in: %s", userSessionBean.getUser() != null ? "Yes" : "No"));
        Logger.getAnonymousLogger().log(Level.INFO, String.format("Text: %s", commentBackingBean.getText()));
        Logger.getAnonymousLogger().log(Level.INFO, String.format("EntryId: %s", commentBackingBean.getEntryId()));
        
        User user = userSessionBean.getUser();
        
        if(user != null){
            Logger.getAnonymousLogger().log(Level.INFO, "Creating comment!");
            imageManagerBean.createCommentOnImage(user, commentBackingBean.getText(), imageManagerBean.getImage(commentBackingBean.getEntryId()));
        }
        
        // Navigate to same page
        return null;
    }
    
    
    
    public UserSessionBean getUserSessionBean() {
        return userSessionBean;
    }

    public void setUserSessionBean(UserSessionBean userSessionBean) {
        this.userSessionBean = userSessionBean;
    }
    
    public CommentBackingBean getCommentBackingBean() {
        return commentBackingBean;
    }

    public void setCommentBackingBean(CommentBackingBean commentBackingBean) {
        this.commentBackingBean = commentBackingBean;
    }
}
